package com.swas.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swas.entity.User;
import com.swas.service.UserService;
import com.swas.util.MD5Util;
import com.swas.util.ResultVO;
import com.swas.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author lxysky
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 登录
    @PostMapping("/login")
    public ResultVO login(@RequestBody User user, HttpSession session){
        User loginInfo = userService.login(user);
        if (loginInfo != null){
            session.setAttribute("userInfo", loginInfo);
            return ResultVO.success("登录成功", loginInfo);
        }
        return ResultVO.fail("登录失败");
    }

    // 登出
    @PostMapping("/logout")
    public ResultVO logout(HttpSession session){
        session.removeAttribute("loginInfo");
        return ResultVO.success("退出成功");
    }

    // 注册
    @PostMapping("/register")
    public ResultVO register(@RequestBody User user){
        user.setPassword(MD5Util.generateMD5(user.getPassword(), 10, user.getUsername()));
        user.setCreatetime(new Date());
        user.setUpdatetime(new Date());
        User regInfo = userService.register(user);
        if (regInfo != null){
            return ResultVO.success("注册成功", regInfo);
        }
        return ResultVO.fail("注册失败");
    }

    @GetMapping("/roleid/{roleId}")
    public ResultVO getUsersByRoleId(@PathVariable Integer roleId) {
        List<User> list = userService.getUsersByRoleId(roleId);
        return ResultVO.success(list);
    }

    @PostMapping
    public ResultVO insertOne(@RequestBody User user) {
        boolean flag = userService.insertOne(user);
        if (flag) {
            return ResultVO.success("添加成功");
        } else {
            return ResultVO.fail("添加失败");
        }
    }

    @GetMapping("/deleteOne/{id}")
    public ResultVO deleteOne(@PathVariable Integer id) {
        boolean flag = userService.deleteOne(id);
        if (flag) {
            return ResultVO.success("删除成功");
        } else {
            return ResultVO.fail("删除失败");
        }
    }

    @GetMapping("/deleteSome/{ids}")
    public ResultVO deleteSome(@PathVariable List<Integer> ids) {
        boolean flag = userService.deleteSome(ids);
        if (flag) {
            return ResultVO.success("删除成功");
        } else {
            return ResultVO.fail("删除失败");
        }
    }

    @PutMapping("/updateOne")
    public ResultVO updateOne(@RequestBody User user) {
        boolean flag = userService.updateOne(user);
        if (flag) {
            return ResultVO.success("修改成功");
        } else {
            return ResultVO.fail("修改失败");
        }
    }

    @GetMapping("/resetPassword/{id}")
    public ResultVO updateOnePassword(@PathVariable Integer id) {
        boolean flag = userService.updateOnePassword(id);
        if (flag) {
            return ResultVO.success("重置成功");
        } else {
            return ResultVO.fail("重置失败");
        }
    }

    @GetMapping("/{id}")
    public ResultVO selectOne(@PathVariable Integer id) {
        User user = userService.selectOne(id);
        if (user != null) {
            return ResultVO.success("查询成功", user);
        } else {
            return ResultVO.fail("查询失败");
        }
    }

    @GetMapping
    public ResultVO selectAll(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "8") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserVO> list = userService.selectAll();
        PageInfo<UserVO> pageInfo = new PageInfo<>(list);
        if (!list.isEmpty()) {
            return ResultVO.success("查询成功", pageInfo);
        } else {
            return ResultVO.fail("查询失败");
        }
    }

    @GetMapping("/selectLike/{pageNum},{pageSize},{like},{role}")
    public ResultVO selectLike(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
            @PathVariable String like, @PathVariable Integer role) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserVO> list = userService.selectLike(like, role);
        PageInfo<UserVO> pageInfo = new PageInfo<>(list);
        if (!list.isEmpty()) {
            return ResultVO.success("查询成功", pageInfo);
        } else {
            return ResultVO.fail("未找到该用户");
        }
    }

}
